#!/bin/sh

# Part of Zombie Navigator
# Copyright © 2015  Zombie Navigator Developers

dir="`dirname "$0"`"
dir="`cd "$dir";pwd`"
cd "$dir/src"


targetDir="./data/internal-pages/docs/api"

../node_modules/.bin/jsdoc --configure ../jsdoc/conf.json --destination "$targetDir"/ --readme ../jsdoc/README.markdown --recurse ../jsdoc/


echo "Removing embedded fonts and scripts..." >&2
rm -rf "$targetDir"/fonts/ "$targetDir"/scripts/ "$targetDir"/*.jsdoc.html "$targetDir"/styles/prettify-*.css

echo "Shrinking generated documentation..." >&2

# Remove dates to make generated docs deterministic
# Remove MSIE-specific codes
# Remove spaces to reduce the addon size
find "$targetDir" -type f -name '*.html' -execdir sh -c 'egrep -v "<script|^\s*$|src=.(https?:)?//|<\!--\[if|<\!\[endif|lang-css.js|linenumber.js" "$1" | sed "`printf "s/^[ \t]*</</"`" | sed "s/ *on [A-Za-z]\{3\} [A-Za-z]\{3\} [0-9]\{1,2\} [0-9]\{1,\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} GMT.*$//" | sed "s/[`printf " \t"`]*href="\""[^."\""]*.jsdoc.html[^"\""]*"\""//g" | sed "s/JSDoc:/Zombie Navigator:/" | tr "\n" "\177" | sed "s/>`printf "\177"`\(<\{0,1\}\)/>\1/g" | tr "\177" "\n" > "$1".tmp && mv -f "$1".tmp "$1"' -- {} \;

# Minimize CSS
# Remove @font-face rules and non-Firefox rules
# XXX: Regex chaos
find "$targetDir" -type f -name '*.css' -execdir sh -c 'egrep -v "^\s*$" "$1" | sed "`printf "s/^[ \t]*//"`" | egrep -v "^-ms-|^-webkit-|^font-family:\s*[^,]*;|^src:|url\(.?\.\./fonts|local\(|^font-weight:\s*normal|^font-style:\s*normal|^/\*.*\*/$" | sed "s/ *: */:/" | sed "s/;\{0,1\} *} */}/" | sed "s/ *\([{,>]\) */\1/g" | sed "s/[`printf " \t"`]\{1,\}/ /g" | tr -d "\n" | perl -pe "s/;\}/}/g ; s/\@font\-face[{][}]//g ; s~/\*.*?\*/~~g ; s/[^}{]+\{\}//g" > "$1".tmp && mv -f "$1".tmp "$1"' -- {} \;


echo "Done?" >&2


# vim: ts=4 noet ai

